package com.youku.plugins.controlbarV4.view.components
{
   import flash.display.Sprite;
   import flash.utils.Dictionary;
   import flash.text.TextField;
   import flash.display.Loader;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.youku.plugins.controlbarV4.view.events.PreviewerEvent;
   import flash.system.LoaderContext;
   import flash.net.URLRequest;
   import com.youku.utils.Tweener;
   import flash.display.BitmapData;
   import flash.display.DisplayObject;
   import flash.geom.Matrix;
   
   public class PreviewEffectDisplayer extends Sprite
   {
      
      public function PreviewEffectDisplayer() {
         super();
      }
      
      private static const LOADER_STATE_READY:String = "Loader_state_ready";
      
      private static const LOADER_STATE_LOADING:String = "Loader_state_loading";
      
      private static const LOADER_STATE_LOADED:String = "Loader_state_loaded";
      
      private static const LOADER_STATE_LOAD_ERROR:String = "Loader_state_load_error";
      
      private static const LOADER_STATE_PRELOAD:String = "Loader_state_preload";
      
      private static const DIRECTION_NONE:String = "direction_none";
      
      private static const DIRECTION_LEFT:String = "direction_left";
      
      private static const DIRECTION_RIGHT:String = "direction_right";
      
      private var _loaderArray:Array;
      
      private var _imagePathArray:Array;
      
      private var _dict:Dictionary;
      
      private var _bitmapWidth:Number = 128;
      
      private var _bitmapHeight:Number = 72;
      
      private var _stateText:TextField;
      
      private var _time:Number = -1;
      
      private var _cardLayer:Sprite;
      
      private var _cardsX:Number = 0;
      
      private var _direction:String;
      
      private var _isFirstShow:Boolean = true;
      
      private var _positionArray:Array;
      
      private var _cardArray:Array;
      
      private var _cardIndexOffset:int;
      
      private var _background:Sprite;
      
      public function init(param1:Array) : void {
         var _loc3_:Loader = null;
         this._loaderArray = new Array();
         this._imagePathArray = param1;
         this._dict = new Dictionary();
         var _loc2_:* = 0;
         while(_loc2_ < this._imagePathArray.length)
         {
            _loc3_ = new Loader();
            _loc3_.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onImageLoaded);
            _loc3_.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onImageLoadError);
            _loc3_.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onImageLoadError);
            this._loaderArray.push(_loc3_);
            this._dict[_loc3_] = LOADER_STATE_READY;
            _loc2_++;
         }
         this.initCards();
      }
      
      private function initCards() : void {
         var _loc3_:Object = null;
         var _loc4_:Card = null;
         this._positionArray = new Array();
         var _loc1_:* = 0;
         while(_loc1_ < 29)
         {
            _loc3_ = new Object();
            _loc3_.x = this._bitmapWidth * _loc1_;
            _loc3_.y = 24;
            _loc3_.s = 1;
            _loc3_.a = 1;
            this._positionArray.push(_loc3_);
            _loc1_++;
         }
         this._background = new Sprite();
         this._background.graphics.beginFill(16777215);
         this._background.graphics.drawRect(-(this._bitmapWidth * this._positionArray.length) / 2,23,this._bitmapWidth * this._positionArray.length,this._bitmapHeight + 2);
         this._background.graphics.endFill();
         this.addChild(this._background);
         this._cardLayer = new Sprite();
         this._cardArray = new Array();
         var _loc2_:* = 0;
         while(_loc2_ < this._positionArray.length)
         {
            _loc4_ = new Card();
            _loc4_.init(this._bitmapWidth,this._bitmapHeight);
            this._cardArray.push(_loc4_);
            this._cardLayer.addChild(_loc4_);
            this._cardsX = this._cardsX + this._positionArray[_loc2_].s * this._bitmapWidth;
            _loc4_.addEventListener(CardEvent.CLICK_SEEK,this.onCardEvent);
            _loc4_.addEventListener(CardEvent.CLICK_SHARE,this.onCardEvent);
            _loc2_++;
         }
         this._cardLayer.x = -int(this._cardsX / 2) + int(this._bitmapWidth / 2);
         this.addChild(this._cardLayer);
         this._cardIndexOffset = Math.floor(this._cardArray.length / 2);
      }
      
      private function onCardEvent(param1:CardEvent) : void {
         var _loc2_:Number = param1.time;
         switch(param1.type)
         {
            case CardEvent.CLICK_SEEK:
               this.dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_CLICK_TIME_CHANGE,{"time":_loc2_}));
               break;
            case CardEvent.CLICK_SHARE:
               this.dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_SHARE,{"time":_loc2_}));
               break;
         }
      }
      
      public function showImage(param1:Number, param2:Boolean = false) : void {
         var _loc3_:Number = this.getNumberFormTime(param1);
         if(this._isFirstShow == true)
         {
            this._direction = DIRECTION_NONE;
         }
         else
         {
            if(_loc3_ > this._time)
            {
               this._direction = DIRECTION_RIGHT;
            }
            if(_loc3_ < this._time)
            {
               this._direction = DIRECTION_LEFT;
            }
            if(_loc3_ == this._time)
            {
               this._direction = DIRECTION_NONE;
            }
         }
         if(_loc3_ == this._time && param2 == false)
         {
            return;
         }
         this.loadImage(_loc3_);
         this.imageMotion();
         this.renderImageArray(_loc3_);
         this._time = _loc3_;
         this._isFirstShow = false;
      }
      
      private function loadImage(param1:int) : void {
         var _loc5_:LoaderContext = null;
         var _loc2_:int = this.getPosition(param1).n;
         var _loc3_:Loader = this._loaderArray[_loc2_] as Loader;
         var _loc4_:String = this._dict[_loc3_];
         if(_loc4_ == LOADER_STATE_READY || _loc4_ == LOADER_STATE_PRELOAD)
         {
            _loc5_ = new LoaderContext(true);
            _loc3_.load(new URLRequest(this._imagePathArray[_loc2_] as String),_loc5_);
            this._dict[_loc3_] = LOADER_STATE_LOADING;
         }
         switch(this._direction)
         {
            case DIRECTION_NONE:
               this.changeStateToPreload([_loc2_ - 1,_loc2_ + 1]);
               break;
            case DIRECTION_RIGHT:
               this.changeStateToPreload([_loc2_ + 1,_loc2_ + 2]);
               break;
            case DIRECTION_LEFT:
               this.changeStateToPreload([_loc2_ - 1,_loc2_ - 2]);
               break;
         }
         this.preloadImage();
      }
      
      private function changeStateToPreload(param1:Array) : void {
         var _loc3_:* = 0;
         var _loc4_:Loader = null;
         var _loc5_:String = null;
         var _loc2_:* = 0;
         while(_loc2_ < param1.length)
         {
            _loc3_ = param1[_loc2_];
            _loc3_ = _loc3_ > 0?_loc3_:0;
            _loc3_ = _loc3_ < this._loaderArray.length - 1?_loc3_:this._loaderArray.length - 1;
            _loc4_ = this._loaderArray[_loc3_] as Loader;
            _loc5_ = this._dict[_loc4_];
            if(_loc5_ == LOADER_STATE_READY)
            {
               this._dict[_loc4_] = LOADER_STATE_PRELOAD;
            }
            _loc2_++;
         }
      }
      
      private function preloadImage() : void {
         var _loc2_:Loader = null;
         var _loc3_:String = null;
         var _loc4_:LoaderContext = null;
         var _loc1_:* = 0;
         while(_loc1_ < this._loaderArray.length)
         {
            _loc2_ = this._loaderArray[_loc1_] as Loader;
            _loc3_ = this._dict[_loc2_];
            if(_loc3_ == LOADER_STATE_PRELOAD)
            {
               _loc4_ = new LoaderContext(true);
               _loc2_.load(new URLRequest(this._imagePathArray[_loc1_] as String),_loc4_);
               this._dict[_loc2_] = LOADER_STATE_LOADING;
            }
            _loc1_++;
         }
      }
      
      private function renderImageArray(param1:int) : void {
         var _loc3_:Card = null;
         var _loc4_:Object = null;
         var _loc2_:* = 0;
         while(_loc2_ < this._cardArray.length)
         {
            _loc3_ = this._cardArray[_loc2_];
            _loc4_ = this.getPosition(param1 + (_loc2_ - this._cardIndexOffset));
            this.updateCard(_loc4_,_loc3_,param1 + (_loc2_ - this._cardIndexOffset));
            _loc2_++;
         }
      }
      
      private function updateCard(param1:Object, param2:Card, param3:Number) : void {
         var _loc4_:Card = param2;
         var _loc5_:Object = param1;
         var _loc6_:Loader = this._loaderArray[_loc5_.n] as Loader;
         var _loc7_:String = this._dict[_loc6_];
         _loc4_.setTime(param3 * 6 - 3);
         switch(_loc7_)
         {
            case LOADER_STATE_LOADED:
               this.renderBitmap(_loc5_,_loc4_.bitmapData);
               break;
            case LOADER_STATE_LOAD_ERROR:
               param2.setError();
               break;
            case LOADER_STATE_READY:
            case LOADER_STATE_LOADING:
            case LOADER_STATE_PRELOAD:
               param2.setLoading();
               break;
         }
      }
      
      private function imageMotion() : void {
         var _loc3_:Card = null;
         var _loc4_:Object = null;
         var _loc5_:Card = null;
         var _loc6_:Object = null;
         var _loc1_:* = 0;
         while(_loc1_ < this._cardArray.length)
         {
            _loc3_ = this._cardArray[_loc1_];
            _loc4_ = this._positionArray[_loc1_];
            Tweener.killTweensOf(_loc3_);
            _loc3_.x = _loc4_.x;
            _loc3_.y = _loc4_.y;
            _loc3_.scaleX = _loc3_.scaleY = _loc4_.s;
            _loc3_.alpha = _loc4_.a;
            _loc1_++;
         }
         switch(this._direction)
         {
            case DIRECTION_RIGHT:
               this._cardArray.push(this._cardArray.shift());
               break;
            case DIRECTION_LEFT:
               this._cardArray.unshift(this._cardArray.pop());
               break;
         }
         var _loc2_:* = 0;
         while(_loc2_ < this._cardArray.length)
         {
            _loc5_ = this._cardArray[_loc2_];
            if(_loc2_ == 0 || _loc2_ == this._cardArray.length - 1)
            {
               _loc5_.visible = false;
            }
            else
            {
               _loc5_.visible = true;
            }
            _loc6_ = this._positionArray[_loc2_];
            Tweener.to(_loc5_,0.5,
               {
                  "x":_loc6_.x,
                  "y":_loc6_.y,
                  "scaleX":_loc6_.s,
                  "scaleY":_loc6_.s,
                  "alpha":_loc6_.a
               });
            _loc5_.isHighlight = false;
            _loc2_++;
         }
         Card(this._cardArray[Math.floor(this._cardArray.length / 2)]).isHighlight = true;
      }
      
      private function getNumberFormTime(param1:Number) : Number {
         var _loc2_:Number = param1;
         _loc2_ = Math.round(_loc2_ / 6) - 1;
         _loc2_ = _loc2_ > 0?_loc2_:0;
         _loc2_ = _loc2_ < this._loaderArray.length * 100?_loc2_:this._loaderArray.length * 100;
         return _loc2_;
      }
      
      private function getPosition(param1:int) : Object {
         var _loc2_:int = param1;
         _loc2_ = _loc2_ > 0?_loc2_:0;
         _loc2_ = _loc2_ < this._loaderArray.length * 100?_loc2_:this._loaderArray.length * 100;
         var _loc3_:int = Math.floor(_loc2_ / 100);
         var _loc4_:int = Math.floor(_loc2_ % 100 % 10);
         var _loc5_:int = Math.floor(_loc2_ % 100 / 10);
         return 
            {
               "n":_loc3_,
               "x":_loc4_,
               "y":_loc5_
            };
      }
      
      private function renderBitmap(param1:Object, param2:BitmapData) : void {
         var _loc3_:Loader = null;
         var _loc4_:DisplayObject = null;
         var _loc5_:Matrix = null;
         try
         {
            _loc3_ = this._loaderArray[param1.n] as Loader;
            _loc4_ = _loc3_.content as DisplayObject;
            _loc5_ = new Matrix(1,0,0,1,-this._bitmapWidth * param1.x,-this._bitmapHeight * param1.y);
            param2.draw(_loc4_,_loc5_);
         }
         catch(error:Error)
         {
         }
      }
      
      private function onImageLoadError(param1:Event) : void {
         var _loc2_:Loader = param1.target.loader as Loader;
         this._dict[_loc2_] = LOADER_STATE_LOAD_ERROR;
         this.preloadImage();
         this.renderImageArray(this._time);
      }
      
      private function onImageLoaded(param1:Event) : void {
         var _loc2_:Loader = param1.target.loader as Loader;
         this._dict[_loc2_] = LOADER_STATE_LOADED;
         this.preloadImage();
         this.renderImageArray(this._time);
      }
      
      public function isCurrentImageLoaded(param1:Number) : Boolean {
         var _loc2_:Number = this.getNumberFormTime(param1);
         var _loc3_:Object = this.getPosition(_loc2_);
         if(this._dict[this._loaderArray[_loc3_.n]] == LOADER_STATE_LOADED)
         {
            return true;
         }
         return false;
      }
      
      public function hide() : void {
         this._isFirstShow = true;
      }
      
      public function get bottomCards_x() : Number {
         return this._cardsX;
      }
      
      public function get bottomCard() : Sprite {
         return this._cardLayer;
      }
   }
}
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.text.TextField;
import com.youku.baseUI.BasePlayerButton;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import com.youku.utils.AssetUtil;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import com.youku.utils.BaseUtil;

class Card extends Sprite
{
   
   function Card() {
      super();
   }
   
   private var _bmpLayer:Sprite;
   
   private var _bitmapData:BitmapData;
   
   private var _bitmap:Bitmap;
   
   private var _stateText:TextField;
   
   private var _textBg:Sprite;
   
   private var _btn:BasePlayerButton;
   
   private var _time:Number;
   
   private var _timeTF:TextField;
   
   private var _isHighlight:Boolean;
   
   public function init(param1:Number, param2:Number) : void {
      this._bmpLayer = new Sprite();
      this.addChild(this._bmpLayer);
      this._bitmapData = new BitmapData(param1,param2,false,0);
      this._bitmap = new Bitmap(this._bitmapData);
      this._bitmap.smoothing = true;
      this._bmpLayer.addChild(this._bitmap);
      this._textBg = new Sprite();
      this._textBg.graphics.beginFill(0);
      this._textBg.graphics.drawRect(0,0,param1,param2);
      this._textBg.graphics.endFill();
      this._textBg.visible = false;
      addChild(this._textBg);
      this._stateText = new TextField();
      this._stateText.defaultTextFormat = new TextFormat("Arial",12,16777215);
      this._stateText.autoSize = TextFieldAutoSize.CENTER;
      this._stateText.width = 100;
      this._stateText.height = 20;
      this._stateText.selectable = false;
      this._stateText.x = (param1 - this._stateText.width) / 2;
      this._stateText.y = (param2 - this._stateText.height) / 2;
      this._stateText.visible = false;
      addChild(this._stateText);
      var _loc3_:TextFormat = new TextFormat();
      _loc3_.font = "Arial";
      _loc3_.size = 13;
      _loc3_.color = 16777215;
      _loc3_.leading = 1;
      _loc3_.align = TextFormatAlign.CENTER;
      this._timeTF = new TextField();
      this._timeTF.defaultTextFormat = _loc3_;
      this._timeTF.selectable = false;
      this._timeTF.multiline = true;
      this._timeTF.wordWrap = true;
      this._timeTF.cacheAsBitmap = true;
      this._timeTF.width = param1;
      this._timeTF.height = 20;
      this._timeTF.x = 0;
      this._timeTF.y = -this._timeTF.height + 2;
      this._timeTF.filters = [new DropShadowFilter(2,45,0,1,2,2,2)];
      this._timeTF.visible = false;
      addChild(this._timeTF);
      this._btn = AssetUtil.getStyleButton("P_share");
      AssetUtil.restoreButtonSize(this._btn);
      this._btn.x = param1 - this._btn.width;
      this._btn.y = 0;
      this._btn.visible = false;
      this._btn.addEventListener(MouseEvent.CLICK,this.onBtnClick);
      addChild(this._btn);
      this.addEventListener(MouseEvent.ROLL_OVER,this.onCardRollOver);
      this.addEventListener(MouseEvent.ROLL_OUT,this.onCardRollOut);
      this._bmpLayer.addEventListener(MouseEvent.CLICK,this.onCardClick);
   }
   
   private function onBtnClick(param1:MouseEvent) : void {
      var _loc2_:CardEvent = new CardEvent(CardEvent.CLICK_SHARE);
      _loc2_.time = this._time;
      this.dispatchEvent(_loc2_);
   }
   
   private function onCardRollOver(param1:MouseEvent) : void {
      this._btn.visible = true;
      this._bitmap.transform.colorTransform = new ColorTransform(0.65,0.65,0.65,1,89,89,89,0);
      this._timeTF.visible = !this._isHighlight;
   }
   
   private function onCardRollOut(param1:MouseEvent) : void {
      this._btn.visible = false;
      this._timeTF.visible = false;
      this._bitmap.transform.colorTransform = new ColorTransform();
   }
   
   private function onCardClick(param1:MouseEvent) : void {
      var _loc2_:CardEvent = new CardEvent(CardEvent.CLICK_SEEK);
      _loc2_.time = this._time;
      this.dispatchEvent(_loc2_);
   }
   
   public function setError() : void {
      this._textBg.visible = true;
      this._stateText.visible = true;
      this._stateText.text = "预览加载失败";
   }
   
   public function setLoading() : void {
      this._textBg.visible = true;
      this._stateText.visible = true;
      this._stateText.text = "加载中";
   }
   
   public function get bitmapData() : BitmapData {
      this._textBg.visible = false;
      this._stateText.visible = false;
      return this._bitmapData;
   }
   
   public function setTime(param1:Number) : void {
      this._timeTF.text = BaseUtil.formatTime(param1 + 6);
      this._time = param1 + 6;
   }
   
   public function get isHighlight() : Boolean {
      return this._isHighlight;
   }
   
   public function set isHighlight(param1:Boolean) : void {
      if(param1)
      {
         this._timeTF.visible = false;
      }
      this._isHighlight = param1;
   }
}
import flash.events.Event;

class CardEvent extends Event
{
   
   function CardEvent(param1:String) {
      super(param1);
   }
   
   public static const CLICK_SEEK:String = "CLICK_SEEK";
   
   public static const CLICK_SHARE:String = "CLICK_SHARE";
   
   public var time:Number;
}
